using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Acms.WebControls
{
    public class CommandField : DataControlField
    {
        protected override DataControlField CreateField()
        {
            return new CommandField();
        }

        private ButtonType buttonType;
        public ButtonType ButtonType
        {
            get { return buttonType; }
            set { buttonType = value; }
        }

        private string text;
        public string Text
        {
            get { return text; }
            set { text = value; }
        }

        private string commandName;
        public string CommandName
        {
            get { return commandName; }
            set { commandName = value; }
        }

        private string confirmMessage;
        public string ConfirmMessage
        {
            get { return confirmMessage; }
            set { confirmMessage = value; }
        }

        private bool readOnlyVisible = true;
        public bool ReadOnlyVisible
        {
            get { return readOnlyVisible; }
            set { readOnlyVisible = value; }
        }

        private bool insertVisible = true;
        public bool InsertVisible
        {
            get { return insertVisible; }
            set { insertVisible = value; }
        }

        private bool editVisible = true;
        public bool EditVisible
        {
            get { return editVisible; }
            set { editVisible = value; }
        }

        private bool selectedVisible = true;
        public bool SelectedVisible
        {
            get { return selectedVisible; }
            set { selectedVisible = value; }
        }
	

        private string headerText;
        public string HeaderText
        {
            get { return headerText; }
            set { headerText = value; }
        }

        private string imageUrl;
        public string ImageUrl
        {
            get { return imageUrl; }
            set { imageUrl = value; }
        }

        private bool causesValidation;
        public bool CausesValidation
        {
            get { return causesValidation; }
            set { causesValidation = value; }
        }

        private string validationGroup;
        public string ValidationGroup
        {
            get { return validationGroup; }
            set { validationGroup = value; }
        }
	

        public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
        {
            if ((rowState & DataControlRowState.Edit) > 0 && !EditVisible)
            {
                cell.Visible = false;
            }
            else if ((rowState & DataControlRowState.Insert) > 0 && !InsertVisible)
            {
                cell.Visible = false;
            }
            else if (((rowState & DataControlRowState.Normal) > 0 || (rowState & DataControlRowState.Alternate) > 0) && !ReadOnlyVisible)
            {
                cell.Visible = false;
            }
            else if ((rowState & DataControlRowState.Selected) > 0 && !SelectedVisible)
            {
                cell.Visible = false;
            }
            else
            {
                if (cellType == DataControlCellType.Header)
                {
                    if (!string.IsNullOrEmpty(HeaderText))
                    {
                        cell.Text = HeaderText;
                    }
                }
                else if (cellType == DataControlCellType.DataCell)
                {
                    IButtonControl btn = null;
                    if (ButtonType == ButtonType.Link)
                    {
                        btn = new LinkButton();
                        if (!string.IsNullOrEmpty(ConfirmMessage))
                        {
                            ((LinkButton)btn).OnClientClick = string.Format("javascript:return confirm('{0}');", ConfirmMessage);
                        }
                    }
                    else if (ButtonType == ButtonType.Button)
                    {
                        btn = new Button();
                        if (!string.IsNullOrEmpty(ConfirmMessage))
                        {
                            ((Button)btn).OnClientClick = string.Format("javascript:return confirm('{0}');", ConfirmMessage);
                        }
                    }
                    else if (ButtonType == ButtonType.Image)
                    {
                        btn = new ImageButton();
                        ((ImageButton)btn).ImageUrl = ImageUrl;
                        ((ImageButton)btn).ToolTip = Text;
                        if (!string.IsNullOrEmpty(ConfirmMessage))
                        {
                           ((ImageButton)btn).OnClientClick = string.Format("javascript:return confirm('{0}');", ConfirmMessage);
                        }
                    }
                    if(btn != null)
                    {
                        btn.Text = Text;
                        btn.CommandName = CommandName;
                        btn.CommandArgument = rowIndex.ToString();
                        btn.CausesValidation = CausesValidation;
                        btn.ValidationGroup = ValidationGroup;
                        cell.Controls.Add((Control)btn);
                    }
                }
            }
        }
	
	
    }
}
